/* 
 * File:   xmlprocessor.cpp
 * Author: robbiediaz
 * 
 * Created on January 28, 2009, 2:32 PM
 */

#include "xmlprocessor.h"

XmlProcessor::XmlProcessor() {
    try {
        XMLPlatformUtils::Initialize();
    } catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        std::cout << "Error during initialization! :\n" << message << std::endl;
        XMLString::release(&message);
    }

    parser = new XercesDOMParser();
    errHandler = (ErrorHandler*) new HandlerBase();

    parser->setValidationScheme(XercesDOMParser::Val_Always);
    parser->setErrorHandler(errHandler);
}

XmlProcessor::~XmlProcessor() {
    XMLPlatformUtils::Terminate();
}

void XmlProcessor::parseStream(std::string input)
{
    XMLCh* str = XMLString::transcode(input.c_str());

    TranscodeToStr tts(str, "UTF-8");

    MemBufInputSource source(tts.str(), tts.length(), "1");

    try {
        parser->parse(source);
        DOMDocument *root = parser->getDocument();

        DOMNode *node = root->getFirstChild();

        char* name = XMLString::transcode(node->getNodeName());
        std::cout << "Node Name: " << name << std::endl;
        std::cout << "Node Type: " << node->getNodeType() << std::endl;

        XMLString::release(&name);
    } catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        std::cout << "Exception message is: \n" << message << std::endl;
        XMLString::release(&message);
    } catch (const DOMException& toCatch) {
        char* message = XMLString::transcode(toCatch.msg);
        std::cout << "Exception message is: \n" << message << std::endl;
        XMLString::release(&message);
    } catch (SAXParseException& e) {
        std::cout << "Message: " << XMLString::transcode(e.getMessage()) << std::endl;
        std::cout << "Column Number: " << e.getColumnNumber() << std::endl;
        std::cout << "Line Number: " << e.getLineNumber() << std::endl;
        std::cout << "Public ID: " << XMLString::transcode(e.getPublicId()) << std::endl;
        std::cout << "System ID: " << XMLString::transcode(e.getSystemId()) << std::endl;
    }
}